//
//  ZHPlayListViewController.m
//  XMLYFM
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 zzl. All rights reserved.
//

#import "ZHPlayListViewController.h"
#import "BaseView.h"
#import "ZHPlayListTableViewCell.h"
#import "ZTAlbumModel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "ZHPlayViewController.h"

@interface ZHPlayListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) UITableView *listTableView;

@end

@implementation ZHPlayListViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.view bringSubviewToFront:self.messageView];
    self.messageView.center = CGPointMake(SCREEN_WIDTH_ZH/2,  SCREEN_HEIGHT_ZH *0.85);
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTableView];
}
- (void)createTableView{
    _listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH_ZH, SCREEN_HEIGHT_ZH - 64) style:UITableViewStylePlain];
    _listTableView.delegate = self;
    _listTableView.dataSource = self;
    [self.view addSubview:_listTableView];
    [_listTableView registerClass:[ZHPlayListTableViewCell class] forCellReuseIdentifier:@"ZHPlayListTableViewCell"];
    
    BaseButton *backBtn = [BaseButton buttonWithType:UIButtonTypeCustom];
//    backBtn.frame = CGRectMake(0, self.listTableView.frame.size.height, SCREEN_WIDTH_ZH ,SCREEN_HEIGHT_ZH-self.listTableView.frame.size.height);
    [backBtn setTitle:@"返回到播放页面" forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor colorWithRed:0.98 green:0.35 blue:0.16 alpha:1] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.bottom.mas_equalTo(@(-10));
        make.left.mas_equalTo(@20);
        make.height.mas_equalTo(@30);
    }];
    
    
    BaseView *headView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH_ZH, 30)];
    UIImageView *arrowImageView = [[UIImageView alloc] init];

    arrowImageView.backgroundColor = [UIColor clearColor];
    arrowImageView.image = [UIImage imageNamed:@"findsection_logo@2x.png"];
    [headView addSubview:arrowImageView];
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = _kind;
    [headView addSubview:titleLabel];
    
    self.listTableView.tableHeaderView = headView;
   [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
       make.left.top.equalTo(headView).offset(10);
       make.size.mas_equalTo(CGSizeMake(10, 10));
   }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(arrowImageView).offset(20);
        make.top.equalTo(arrowImageView);
        make.size.mas_equalTo(CGSizeMake(120, 10));
    }];
    
}
- (void)backBtnAction:(BaseButton *)btn{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}
#pragma mark - tableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.playListArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"ZHPlayListTableViewCell";
    ZHPlayListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    ZTAlbumModel *model = _playListArray[indexPath.row];
    [cell.newsPic sd_setImageWithURL:[NSURL URLWithString:model.coverSmall] placeholderImage:PLACEHOLDER_IMAGE];
    cell.newsTitle.text = model.title;
    cell.newsTitle.font = [UIFont systemFontOfSize:15.f];
   
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZHPlayViewController *playerVC = [[ZHPlayViewController alloc]init];
    ZHNavigationBarController *naVC = [[ZHNavigationBarController alloc]initWithRootViewController:playerVC];
    ZTAlbumModel *model = _playListArray[indexPath.row];
    playerVC.song_id = model.trackId;
    playerVC.allMusicArray = _playListArray;
    playerVC.indexRow = indexPath.row;
    [self presentViewController:naVC animated:YES completion:^{
        
        
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
